<template>
  <div class="log-download">
    <el-dialog
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
      title="下载日志"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="120px"
      >
        <el-form-item label="选择日期" prop="date">
          <el-date-picker
            type="date"
            v-model="formData.date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder='选择日期时间'
            :default-value="defaultTime"
            :picker-options="pickerOptions"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          type="success"
          :loading="loading"
          @click="sendUpdateLog"
        >
          {{ loading ? `${inTime}s` : "获取" }}
        </el-button>
        <el-button
          type="primary"
          v-disable-click="handleConfirm"
          :disabled="disabledDownload"
          >下载 </el-button
        >
        <el-button @click="close">{{ $t("cancelText") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
const TIME_OUT = 30; // 超时时间
import {sendDoorMsg} from "@/api/door/device";

export default {
  name: "LogDownload",
  props: {
    defaultDevice: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      inTime: TIME_OUT, // 还剩多少秒
      formData: {
        date: new Date().toISOString().split('T')[0],
      },
      rules: {
        date: [
          {
            required: true,
            message: "选择日期不能为空",
            trigger: "change",
          },
        ],
      },
      defaultTime: new Date(),
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      timer: null, // 超时定时器
      disabledDownload: false,
      loading: false,
      initMqtt: false, // 是否初始化mqtt
    };
  },
  computed: {
  },
  mounted() {
    this.disabledDownload = false;
    this.loading = false;
    clearTimeout(this.timer);
  },
  methods: {
    async onClose() {
      console.log("kkkk")
      this.$refs["elForm"].resetFields();
      setTimeout(() => {
        this.initMqtt = false;
        clearTimeout(this.timer);
      }, 300);
    },
    onOpen() {
      this.disabledDownload = false;
      this.loading = false;
    },
    close() {
      this.$emit("update:visible", false);
    },
    sendUpdateLog() {
      console.log("zczczczc",this.defaultDevice.deviceCode)
      const msg = {date: this.formData.date};
      const topic = 'door/server/'+this.defaultDevice.deviceCode+'/getLog';
      let param = {
        topic: topic,
        message: JSON.stringify(msg)
      }
      console.info("🚀 ~ 发布消息队列 !", topic);
      console.info("🚀 ~ 发布消息消息 !", msg);
      sendDoorMsg(param);
      this.$notify({
        title: this.$t("指令发送成功"),
        message: this.$t("已发送通知安卓端上传日志指令, 请等待响应结果~"),
        type: "success",
      });

      this.loading = true;
      this.disabledDownload = true;
      this.inTime = TIME_OUT;

      this.timer = setInterval(() => {
        if (this.inTime <= 0) {
          this.$notify({
            title: this.$t("日志上传"),
            message: this.$t("日志上传中, 请稍后获取日志或者直接下载~"),
            type: "warning",
          });
          this.disabledDownload = false;
          this.loading = false;
          clearTimeout(this.timer);
          return;
        }
        this.inTime -= 1;
      }, 1000);
    },
    handleConfirm() {
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.$emit("handleConfirm", { ...this.formData });
      });
    },
  },
};
</script>
<style lang="scss">
.log-download {
  .el-dialog__body {
    display: flex;
    justify-content: center;
  }
  .el-dialog__footer {
    display: flex;
    justify-content: center;
  }
}
</style>
